//
//  SearchBarViewController.m
//  网易公开课项目
//
//  Created by 林 艺 on 13-11-19.
//  Copyright (c) 2013年 Marklin. All rights reserved.
//

#import "SearchBarViewController.h"
#import "LoadCourseDataManager.h"
#import "CourseModel.h"
#import "SearchResultCell.h"
#import "DetailViewController.h"
#import "SearchResultViewController.h"

@interface SearchBarViewController ()

@end

@implementation SearchBarViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.title = @"搜索课程";
        
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    _searchArray = [[NSMutableArray alloc] init];
    [self _initNavigationBarItem];
    [self _initSearchBar];
    

}

- (void)_initSearchBar
{
    _searchBar = [[CourseSearchBar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
    _searchBar.delegate = self;
    //    [_searchBar setBackgroundImage:[UIImage imageNamed:@"search_bg.png"]];
    //    [_searchBar setSearchFieldBackgroundImage:[UIImage imageNamed:@"search.png"] forState:UIControlStateNormal];
    
    _searchDisplay = [[UISearchDisplayController alloc] initWithSearchBar:_searchBar contentsController:self];
    _searchDisplay.searchResultsDataSource = self;
    _searchDisplay.searchResultsDelegate = self;
    _searchDisplay.searchResultsTableView.backgroundColor = [UIColor clearColor];
    _searchDisplay.searchResultsTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [_searchDisplay.searchResultsTableView registerNib:[UINib nibWithNibName:@"SearchResultCell" bundle:nil] forCellReuseIdentifier:@"SearchResultCell"];
    [_tableView setTableHeaderView:_searchBar];
}

- (void)_initNavigationBarItem
{
    //返回
    UIButton *back = [UIButton buttonWithType:UIButtonTypeCustom];
    [back setBackgroundImage:[UIImage imageNamed:@"btn_left.png"] forState:UIControlStateNormal];
    
    [back setTitle:@"返回" forState:UIControlStateNormal];
    back.titleLabel.font = [UIFont systemFontOfSize:11];
    [back addTarget:self action:@selector(backClicked) forControlEvents:UIControlEventTouchUpInside];
    back.frame = CGRectMake(0, 0, 50, 34);
    UIBarButtonItem *leftButton = [[UIBarButtonItem alloc] initWithCustomView:back];
    self.navigationItem.leftBarButtonItem = leftButton;
}

- (void)backClicked
{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (_searchArray != 0)
    {
        [_searchArray removeAllObjects];
        _searchArray = nil;
        _searchArray = [[NSMutableArray alloc] init];
    }

    LoadCourseDataManager *dataManger = [LoadCourseDataManager shareInstance];
    if (_searchBar.text == nil)
    {
        return 0;
    }
    for (CourseModel *model in dataManger.courseData)
    {
        NSString *str = model.title;
        NSRange range;
        range = [str rangeOfString:_searchBar.text];
        if (range.location != NSNotFound)
        {
            [_searchArray addObject:model];
        }
    }
    
    
    _searchDisplay.searchResultsTableView.backgroundColor = [UIColor clearColor];
    _searchDisplay.searchResultsTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [_searchDisplay.searchResultsTableView registerNib:[UINib nibWithNibName:@"SearchResultCell" bundle:nil] forCellReuseIdentifier:@"SearchResultCell"];
    
    return _searchArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    SearchResultCell *cell = [tableView dequeueReusableCellWithIdentifier:@"SearchResultCell"];
    CourseModel *model = [_searchArray objectAtIndex:indexPath.row];
    cell.resultLabel.text = model.title;
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    DetailViewController *detailController = [[DetailViewController alloc] initWithNibName:@"DetailViewController" bundle:nil];
    CourseModel *model = [_searchArray objectAtIndex:indexPath.row];
    detailController.videoCode = model.plid;
    //隐藏tabbarView;
    UIView * view =[self.tabBarController valueForKey:@"tabbarView"];
    view.frame = CGRectMake(-320, view.frame.origin.y, view.frame.size.width, view.frame.size.height);
    [self.navigationController pushViewController:detailController animated:YES];
}

#pragma mark --
#pragma mark UISearchBarDelegate
- (BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar
{
    self.logoView.hidden = YES;
    [_searchBar setShowsCancelButton:YES];
    for (UIView *searchbuttons in _searchBar.subviews)
    {
        if ([searchbuttons isKindOfClass:[UIButton class]])
        {
            UIButton *cancelButton = (UIButton*)searchbuttons;

            cancelButton.enabled = YES;
            [cancelButton setBackgroundImage:[UIImage imageNamed:@"search_btn.png"] forState:UIControlStateNormal];
            [cancelButton setTitle:@"" forState:UIControlStateNormal];
            [cancelButton setBackgroundImage:[UIImage imageNamed:@"search_btn_active.png"] forState:UIControlStateHighlighted];
            break;
        }
    }
    
    return YES;
}

- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar
{
    self.logoView.hidden = NO;

}

- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
{
    SearchResultViewController *searchResultController = [[SearchResultViewController alloc] initWithNibName:@"SearchResultViewController" bundle:nil];
    searchResultController.resultArray = _searchArray;
    [self.navigationController pushViewController:searchResultController animated:YES];
}

#pragma mark --
#pragma mark viewWillAppear
-(void)viewWillAppear:(BOOL)animated
{
    //设置全屏
    UIView *tabbarContentView=[self.tabBarController valueForKey:@"viewControllerTransitionView"];
    tabbarContentView.frame=CGRectMake(0, 0, 320, 480);
}



@end
